<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addCateVisible = true"
          >添加分类</el-button
        >
      </div>
      <el-table border stripe :data="cateList" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="primary"
              @click="showEditDialog(scope.row)"
              >修改</el-button
            >
            <el-button size="mini" type="danger" @click="del(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加文章分类对话框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="addCateVisible"
      width="35%"
      @closed="onAddCateClose"
      :close-on-click-modal="false"
    >
      <!-- 表单 -->
      <el-form
        :model="addCateForm"
        :rules="addCateRules"
        ref="addCateFormRef"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addCateForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input
            v-model="addCateForm.cate_alias"
            @keydown.enter.native="addCate"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑文章分类对话框 -->
    <el-dialog title="编辑文章分类" :visible.sync="editCateVisible" width="35%">
      <!-- 表单 -->
      <el-form
        :model="editCateForm"
        :rules="addCateRules"
        ref="editCateFormRef"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editCateForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input
            v-model="editCateForm.cate_alias"
            @keydown.enter.native="editCate"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ArtCate",
  data() {
    return {
      // 文章的分类列表
      cateList: [],
      //控制添加文章分类对话框显示和隐藏
      addCateVisible: false,
      addCateForm: {
        cate_name: "",
        cate_alias: "",
      },
      // 表单校验规则
      addCateRules: {
        cate_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            pattern: /^\S{1,10}$/,
            message: "分类名必须是1-10位的非空字符",
            trigger: "blur",
          },
        ],
        cate_alias: [
          { required: true, message: "请输入分类别名", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: "分类别名必须是1-15位的字母数字",
            trigger: "blur",
          },
        ],
      },
      //控制编辑文章分类对话框显示和隐藏
      editCateVisible: false,
      // 编辑文章分类的数据对象
      editCateForm: {},
    };
  },

  methods: {
    // 获取文章分类
    async getArtCate() {
      const { data: res } = await this.$http.get("/my/cate/list");
      if (res.code !== 0) return this.$message.error(res.message);
      // console.log(res.data);
      this.cateList = res.data;
      // this.$message.success(res.message);
    },
    onAddCateClose() {
      this.$refs.addCateFormRef.resetFields();
    },
    // 添加文章分类
    addCate() {
      // 兜底校验
      this.$refs.addCateFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "/my/cate/add",
          this.addCateForm
        );
        if (res.code !== 0) return this.$message.error(res.message);
        // 关闭对话框
        this.addCateVisible = false;
        // 重新获取文章分类数据
        this.getArtCate();
      });
    },
    // 删除文章分类
    del(id) {
      // 发送ajax
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http({
            method: "delete",
            url: "/my/cate/del",
            params: {
              id: id,
            },
          });
          // console.log(res);
          if (res.code !== 0) return this.$message.error(res.message);
          //删除成功 重新获取
          this.getArtCate();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑文章分类
    showEditDialog(obj) {
      // 将obj这个形参赋值给editCateForm空对象 浅拷贝
      this.editCateForm = { ...obj };
      // 将对话框设置为true
      this.editCateVisible = true;
    },
    editCate() {
      // 对表单进行兜底校验
      this.$refs.editCateFormRef.validate(async (valid) => {
        if (!valid) return;
        // 成功 发送ajax
        const { data: res } = await this.$http({
          method: "put",
          url: "/my/cate/info",
          data: {
            ...this.editCateForm,
          },
        });
        // 判断业务状态码
        if (res.code !== 0) return this.$message.error(res.message);
        // 成功
        this.$message.success(res.message);
        // 重新获取
        this.getArtCate();
        // 关闭对话框
        this.editCateVisible = false;
      });
    },
  },
  created() {
    // 调用获取文章分类
    this.getArtCate();
  },
};
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>